package com.wutongyu.mannyburger;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

public class ForgotPasswordFragment extends Fragment {

    private static final String TAG = "ForgotPasswordFragment";
    private EditText editTextPhone;
    private Button buttonVerify;
    private TextView textViewBackToLogin;
    private UserDatabaseHelper dbHelper;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_forgot_password, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        editTextPhone = view.findViewById(R.id.edit_text_phone);
        buttonVerify = view.findViewById(R.id.button_verify);
        textViewBackToLogin = view.findViewById(R.id.text_view_back_to_login);

        dbHelper = new UserDatabaseHelper(requireContext());

        buttonVerify.setOnClickListener(v -> verifyPhone());
        textViewBackToLogin.setOnClickListener(v -> navigateBackToLogin());
    }

    private void verifyPhone() {
        String phone = editTextPhone.getText().toString().trim();

        // 基本验证
        if (TextUtils.isEmpty(phone)) {
            editTextPhone.setError("手机号不能为空");
            editTextPhone.requestFocus();
            return;
        }

        // 检查用户是否存在
        if (!dbHelper.isUserExists(phone)) {
            Toast.makeText(getContext(), "该手机号未注册", Toast.LENGTH_SHORT).show();
            Log.w(TAG, "验证手机号失败，手机号不存在: " + phone);
            return;
        }

        // 检查是否设置了密保问题
        int securityQuestionId = dbHelper.getSecurityQuestion(phone);
        if (securityQuestionId == -1) {
            Toast.makeText(getContext(), "该账户未设置密保问题，无法重置密码", Toast.LENGTH_SHORT).show();
            Log.w(TAG, "用户未设置密保问题: " + phone);
            return;
        }

        // 导航到密保问题验证页面
        navigateToSecurityQuestion(phone, securityQuestionId);
    }

    private void navigateToSecurityQuestion(String phone, int questionId) {
        SecurityQuestionFragment fragment = SecurityQuestionFragment.newInstance(phone, questionId);
        
        // 跳转到密保问题验证页面
        FragmentManager fragmentManager = getParentFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.nav_host_fragment_container, fragment);
        transaction.addToBackStack(null); // 允许用户按返回键回到上一个页面
        transaction.commit();
    }

    private void navigateBackToLogin() {
        Log.d(TAG, "返回登录 Fragment");
        // 从后台堆栈中弹出当前 Fragment，显示之前的 Fragment (LoginFragment)
        getParentFragmentManager().popBackStack();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        // 关闭数据库连接
        if (dbHelper != null) {
            dbHelper.close();
        }
    }
} 